package com.cf.module.app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.cf.base.comm.widget.watermark.WatermarkUtils;
import com.cf.base.comm.widget.watermark.WatermarkView;
import com.cf.base.http.HttpManager;
import com.cf.base.sp.SpCore;
import com.cf.base.sp.annotation.Level;
import com.cf.module.app.bean.UserInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yangji
 */
@Route(path = "/app/main")
public class MainActivity extends AppCompatActivity {

    private TextView tvContent;
    private AFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_module_activity_main);
        tvContent = findViewById(R.id.etContent);
        HttpManager.getInstance();
    }

    public void btnStartNewsFeed(View view) {
        ARouter.getInstance().build("/newsfeed/module/home").navigation(this);
    }

    public void btnStartAppFeed(View view) {
        ARouter.getInstance().build("/app/login").navigation(this, new NavCallback() {
            @Override
            public void onArrival(Postcard postcard) {
                System.out.println("");
            }
        });
    }

    public void btnDialogFragment(View view) {
        BaseDialogFragment fragment = new BaseDialogFragment();
        fragment.show(getSupportFragmentManager(), BaseDialogFragment.class.getName());
    }

    public void updateText(View view) {
        WatermarkView watermarkView = findViewById(R.id.comm_lib_watermark_view_id);
        if (watermarkView != null) {
            watermarkView.setWatermarkText("我更新了数据" + System.currentTimeMillis());
        }
    }

    public void updateTextAll(View view) {
        WatermarkUtils.singleton.get().setWatermarkText("全局Activit 更新" + System.currentTimeMillis()).update();
    }

    public void saveSp(View view) {

        SpCore.put(Level.System, "defaultTest", tvContent.getText().toString());

        SpCore.put(Level.User, "defaultUserTest", "uservalue-" + tvContent.getText().toString());

        UserInfo info = new UserInfo("admin", "1234556");
        SpCore.putUser("userInfo", info);

        Map<String, UserInfo> userMap = new HashMap<>();
        userMap.put("张三", new UserInfo("张三", "123"));
        userMap.put("李四", new UserInfo("李四", "456"));
        SpCore.putCache("userMap", userMap);

        if (fragment == null) {
            fragment = new AFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.flContent, fragment).show(fragment).commit();
        }

    }

    public void querySp(View view) {
        String text = SpCore.get(Level.System, "defaultTest");
        tvContent.setText(text);
        System.out.println("杨吉—text:" + text);

        String userTest = SpCore.get(Level.User, "defaultUserTest");
        System.out.println("杨吉—userText:" + userTest);

        UserInfo info = SpCore.getUser("userInfo");
        System.out.println("杨吉-userInfos" + JSON.toJSONString(info));

        Map<String, UserInfo> userMap = SpCore.getCache("userMap", new HashMap<>());
        System.out.println("杨吉-userMap" + JSON.toJSONString(userMap));
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().remove(fragment).commit();
            fragment = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SpCore.onDestroy();
    }

    public void clickConfig(View view) {
        Config config = (Config) ARouter.getInstance().build("/app/config").navigation();
        config.show();

        Config2 config2 = (Config2) ARouter.getInstance().build("/app/config2").navigation();
        config2.show();
        Fragment fragment = (Fragment) ARouter.getInstance().build("/app/arouterFragment").navigation();

        ARouterService service = (ARouterService) ARouter.getInstance().build("ARouterService").navigation();
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
    }
}
